package com.learning.jquizenglish.screen;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.common.JQuizEnglishFragment;
import com.learning.jquizenglish.common.JQuizViewPagerAdapter;
import com.learning.jquizenglish.model.PageItemInfo;

public class MainScreen extends JQuizEnglishFragment{

	private ViewPager mViewPager;
	private JQuizViewPagerAdapter mAdapter;
	private ArrayList<PageItemInfo> mListPage;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.screen_main, container, false);
		mViewPager = (ViewPager) v.findViewById(R.id.pager);
		initData();
		return v;
	}
	
	private void initData(){
		mListPage = getListPage();
		mAdapter = new JQuizViewPagerAdapter(getFragmentManager(), mListPage);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setCurrentItem(1);
	}
	
	public void setCurrentPage(int page){
		mViewPager.setCurrentItem(page);
	}
	
	private ArrayList<PageItemInfo> getListPage(){
		ArrayList<PageItemInfo> listPage = new ArrayList<PageItemInfo>();
		PageItemInfo profilePage = new PageItemInfo();
		profilePage.spannableStringBuilder = getIndicator(R.drawable.ic_menu_profile);
		profilePage.fragClass = ProfileScreen.class;
		
		PageItemInfo homePage = new PageItemInfo();
		homePage.spannableStringBuilder = getIndicator(R.drawable.ic_menu_home);
		homePage.fragClass = HomeScreen.class;
		
		PageItemInfo learningFeedPage = new PageItemInfo();
		learningFeedPage.spannableStringBuilder = getIndicator(R.drawable.ic_menu_learningfeed);
		learningFeedPage.fragClass = LearningFeedScreen.class;
		
		listPage.add(profilePage);
		listPage.add(homePage);
		listPage.add(learningFeedPage);
		return listPage;
	}
	
	private SpannableStringBuilder getIndicator(int resIcon){
		Drawable drawable = getResources().getDrawable(resIcon);
		SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("  ");
	    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
	    spannableStringBuilder.setSpan(new ImageSpan(drawable, 0), 0, 1, 18);
	    return spannableStringBuilder;
	}

}
